package com.zrrd.yunchmall.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zrrd.yunchmall.user.entity.*;
import com.zrrd.yunchmall.user.mapper.RoleMapper;
import com.zrrd.yunchmall.user.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author LiYe
 * @since 2024-01-15
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private IRoleMenuRelationService roleMenuRelationService;
    @Autowired
    private IRoleResourceRelationService roleResourceRelationService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IResourceService resourceService;


    /**
     * 查询某个角色id对应的菜单列表
     * @param roleId 角色id
     * @return
     */
    @Override
    // 你想要缓存返回结果的方法前都可以使用这个注解，也就是说加上了@Cacheable的方法，返回值会保存到redis缓存中
    // Role:listMenu_5
    /**
     * 该方法的返回结果将会使用Redis进行缓存，缓存的过期时间为5min，缓存序列化方式为JSON
     */
    @Cacheable(value = "Role", key = "#root.methodName+'_'+#roleId", cacheManager = "cacheManager5Minute")
    public List<Menu> listMenu(int roleId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id",roleId);
        queryWrapper.select("menu_id");
        // select menu_id from ums_role_menu_relation where role_id = ?
        List<Long> menuIds = roleMenuRelationService.listObjs(queryWrapper);

        if (menuIds.isEmpty()) {
            return new LinkedList();
        }
        return menuService.listByIds(menuIds);
    }

    @Override
    @Cacheable(value = "Role", key = "#root.methodName+'_'+#root.args[0]", cacheManager = "cacheManager1Minute")
    public List<Resource> listResource(int id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id",id);
        queryWrapper.select("resource_id");
        List<Long> resourceIds = roleResourceRelationService.listObjs(queryWrapper);
        if (resourceIds.isEmpty()) {
            return new LinkedList();
        }
        return resourceService.listByIds(resourceIds);
    }

    @Override

    @Transactional
    public void allocMenu(long roleId, String menuIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id",roleId);
        roleMenuRelationService.remove(queryWrapper);
        for (String menuId : menuIds.split(",")) {
            RoleMenuRelation roleMenuRelation = new RoleMenuRelation();
            roleMenuRelation.setRoleId(roleId);
            roleMenuRelation.setMenuId(Long.valueOf(menuId));
            roleMenuRelationService.save(roleMenuRelation);
        }
    }

    @Override
    public void allocResource(long roleId, String resourceIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id",roleId);
        roleResourceRelationService.remove(queryWrapper);
        for (String resourceId : resourceIds.split(",")){
            RoleResourceRelation roleResourceRelation = new RoleResourceRelation();
            roleResourceRelation.setRoleId(roleId);
            roleResourceRelation.setResourceId(Long.valueOf(resourceId));
            roleResourceRelationService.save(roleResourceRelation);
        }

    }
}
